$(function(){
    $.ajax({
        url:"/product",
        type:"GET",
        dataType:"json",
        data:{},
        success:function(res){
            for(var i = 0;i<res.length;i++){
                var html = '<div class="col-md-3 col-md-offset-2 pro-list">\n' +
                    '            <h2 class="text-center pro-name">'+res[i].proName+'</h2>\n' +
                    '            <img class="img-thumbnail pro-img" src="'+res[i].proImg+'" />\n' +
                    '            <h4 class="pro-price text-center">$'+res[i].proPrice+'</h4>\n' +
                    '            <button class="btn btn-danger col-md-12" onclick="butPro('+res[i].proId+')">立即购买</button>\n' +
                    '        </div>';
                $("#writerList").append(html);
            }
        },
        error:function(err){
            console.log(err);
        }
    })
})

function butPro(id){
    $.ajax({
        url:"/saveOrder",
        type:"post",
        dataType:"json",
        data:{
            proId:id
        },
        success:function(res){
            $(".toast").show();
            setTimeout(function(){
                $(".toast").hide();
            },2000)
        },
        error:function(err){
            console.log(err);
        }
    })
}